//
//  CollectionCellFour.m
//  
//
//  Created by dllo on 16/1/27.
//
//

#import "CollectionCellFour.h"
#import "TableCellOneForCollectionFour.h"
#import "ModelForCollectionFour.h"
#import "TableCellTwoForCollectionViewFour.h"

#import "BangDanViewController.h"
#import "ZhuBoBangDanViewController.h"

#import "NetworkHandlerBlock.h"
#import "ModelForBangDan.h"
#import "ModeForZhuBoBangDan.h"
#import "WebViewController.h"
#import "ModelForListen.h"

@interface CollectionCellFour ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *arrForZuiHuoJieMuBiaoShengBang;
@property (nonatomic, retain) NSMutableArray *arrForZuiDuoDingYueJingDianBang;
@property (nonatomic, retain) NSMutableArray *arrForYouShengXiaoShuoFengYunbang;
@property (nonatomic, retain) NSMutableArray *arrForZuiChaoWaiYuShouTingBang;
@property (nonatomic, retain) NSMutableArray *arrForZuiShouHuanYingGongKaiKeBang;


@property (nonatomic, retain) NSMutableArray *arrForNvShenBang;
@property (nonatomic, retain) NSMutableArray *arrForNanShenBang;
@property (nonatomic, retain) NSMutableArray *arrForXinRenBang;

@end

@implementation CollectionCellFour


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.arrForZuiHuoJieMuBiaoShengBang = [NSMutableArray array];
        self.arrForZuiDuoDingYueJingDianBang = [NSMutableArray array];
        self.arrForYouShengXiaoShuoFengYunbang = [NSMutableArray array];
        self.arrForZuiChaoWaiYuShouTingBang = [NSMutableArray array];
        self.arrForZuiShouHuanYingGongKaiKeBang = [NSMutableArray array];
        
        self.arrForNvShenBang = [NSMutableArray array];
        self.arrForNanShenBang = [NSMutableArray array];
        self.arrForXinRenBang = [NSMutableArray array];
//        if (self.arrForCollectionViewFour) {
            [self createSubViews];
//        }else {
//            UIImageView *backGround = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"wuwang"]];
//            backGround.frame = self.bounds;
//            [self addSubview:backGround];
//            [backGround release];
//        
//        }
        
        [self handleData];
    }
    return self;
}


- (void)handleData {
  
    NSString *strZuiHuoJieMuBiaoShengBang = @"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/track?device=android&key=ranking%3Atrack%3Aplayed%3A1%3A0&pageId=1&pageSize=40";
    
    [NetworkHandlerBlock netWorkingWithURL:[strZuiHuoJieMuBiaoShengBang stringByRemovingPercentEncoding] completeHandle:^(NSURLResponse *response, NSData *data, id result) {
     
        NSArray *tempArr = [result objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            //  ////
            ModelForListen *model = [[ModelForListen alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrForZuiHuoJieMuBiaoShengBang addObject:model];
        }
    }];
    
    
    NSString *strZuiDuoDingYueJingDianBang = @"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/album?device=android&key=ranking%3Aalbum%3Asubscribed%3A30%3A0&pageId=1&pageSize=40";
    [NetworkHandlerBlock netWorkingWithURL:[strZuiDuoDingYueJingDianBang stringByRemovingPercentEncoding]  completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        NSArray *tempArr = [result objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            //////////
            ModelForListen *model = [[ModelForListen alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrForZuiDuoDingYueJingDianBang addObject:model];
        }
    }];


    NSString *strYouShengXiaoShuoFengYunbang = @"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/album?device=android&key=ranking%3Aalbum%3Acustom%3A42&pageId=1&pageSize=40";
    [NetworkHandlerBlock netWorkingWithURL:[strYouShengXiaoShuoFengYunbang stringByRemovingPercentEncoding] completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        NSArray *tempArr = [result objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            ///////
            ModelForListen *model = [[ModelForListen alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrForYouShengXiaoShuoFengYunbang addObject:model];
        }
        
    }];
    
    NSString *strZuiChaoWaiYuShouTingBang = @"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/album?device=android&key=ranking%3Aalbum%3Adownloaded%3A7%3A5&pageId=1&pageSize=40";
    [NetworkHandlerBlock netWorkingWithURL:[strZuiChaoWaiYuShouTingBang stringByRemovingPercentEncoding] completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        NSArray *tempArr = [result objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            /////////
            ModelForListen *model = [[ModelForListen alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrForZuiChaoWaiYuShouTingBang addObject:model];
        }
    }];
    
    NSString *strZuiShouHuanYingGongKaiKeBang = @"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/album?device=android&key=ranking%3Aalbum%3Aplayed%3A7%3A27&pageId=1&pageSize=40";
    [NetworkHandlerBlock netWorkingWithURL:[strZuiShouHuanYingGongKaiKeBang stringByRemovingPercentEncoding] completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        NSArray *tempArr = [result objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            ////////
            ModelForListen *model = [[ModelForListen alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrForZuiShouHuanYingGongKaiKeBang addObject:model];
        }
        
    }];
    
    NSString *strNvShenBang = @"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/anchor?device=android&key=ranking%3Aanchor%3Acustom%3A43&pageId=1&pageSize=40";
    [NetworkHandlerBlock netWorkingWithURL:[strNvShenBang stringByRemovingPercentEncoding] completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        NSArray *tempArr = [result objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            ModeForZhuBoBangDan *model = [[ModeForZhuBoBangDan alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrForNvShenBang addObject:model];
        }
    }];
    
    NSString *strNanShenBang = @"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/anchor?device=android&key=ranking%3Aanchor%3Acustom%3A44&pageId=1&pageSize=40";
    [NetworkHandlerBlock netWorkingWithURL:[strNanShenBang stringByRemovingPercentEncoding] completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        NSArray *tempArr = [result objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            ModeForZhuBoBangDan *model = [[ModeForZhuBoBangDan alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrForNanShenBang addObject:model];
        }
    }];
    
    NSString *strXinShengBang = @"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/anchor?device=android&key=ranking%3Aanchor%3Acustom%3A45&pageId=1&pageSize=40";
    [NetworkHandlerBlock netWorkingWithURL:[strXinShengBang stringByRemovingPercentEncoding] completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        NSArray *tempArr = [result objectForKey:@"list"];
        for (NSDictionary *dic in tempArr) {
            ModeForZhuBoBangDan *model = [[ModeForZhuBoBangDan alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrForXinRenBang addObject:model];
        }
    }];
}



#pragma mark createtableView
- (void)createSubViews {
    
     self.tableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
    [self.contentView addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[TableCellOneForCollectionFour class] forCellReuseIdentifier:@"TableViewCellOneForCollectionViewCellFour"];
    [self.tableView registerClass:[TableCellTwoForCollectionViewFour class] forCellReuseIdentifier:@"TableViewCellTwoForCollectionViewCellFour"];
    [self.tableView release];
    
}

#pragma mark tabelViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrForCollectionViewFour.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    ModelForCollectionFour *model = [self.arrForCollectionViewFour objectAtIndex:indexPath.row];
    if (model.pic) {
    TableCellOneForCollectionFour *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellOneForCollectionViewCellFour"];
        [cell passModelForCellOne:model];
    return cell;
    }else {
    TableCellTwoForCollectionViewFour *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellTwoForCollectionViewCellFour"];
        [cell passModelForCellTwo:model];
        return cell;
    }
}


#pragma mark  tableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ModelForCollectionFour *model = [self.arrForCollectionViewFour objectAtIndex:indexPath.row];
    if (model.pic) {
        
        return 200;
        
    }else{
        
        return 140;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *vc = (UIViewController*)object;
    ModelForCollectionFour *model = [self.arrForCollectionViewFour objectAtIndex:indexPath.row];
    
    if (indexPath.row == 0) {
//        WebViewController *web = [[WebViewController alloc]init];
//        web.stringUrl = model.url;
//        [vc.navigationController pushViewController:web animated:YES];
    }
   
    
    if (indexPath.row == 1) {
        BangDanViewController *banDan = [[BangDanViewController alloc]init];
        banDan.arrForBanDan = self.arrForZuiHuoJieMuBiaoShengBang;
        [vc.navigationController pushViewController:banDan animated:YES];
        
        
    }
    if (indexPath.row == 2){
        BangDanViewController *banDan = [[BangDanViewController alloc]init];
        banDan.arrForBanDan = self.arrForZuiDuoDingYueJingDianBang;
          [vc.navigationController pushViewController:banDan animated:YES];
    }
    if (indexPath.row == 3){
        BangDanViewController *banDan = [[BangDanViewController alloc]init];
        banDan.arrForBanDan = self.arrForYouShengXiaoShuoFengYunbang;
          [vc.navigationController pushViewController:banDan animated:YES];
    }
    if (indexPath.row == 4){
        BangDanViewController *banDan = [[BangDanViewController alloc]init];
        banDan.arrForBanDan = self.arrForZuiChaoWaiYuShouTingBang;
          [vc.navigationController pushViewController:banDan animated:YES];
    }
    if(indexPath.row == 5) {
        BangDanViewController *banDan = [[BangDanViewController alloc]init];
        banDan.arrForBanDan = self.arrForZuiShouHuanYingGongKaiKeBang;
        [vc.navigationController pushViewController:banDan animated:YES];
    }
    if (indexPath.row == 6) {
        ZhuBoBangDanViewController *zhuBoBangDan = [[ZhuBoBangDanViewController alloc]init];
        zhuBoBangDan.arrForZhuBoBangDan = self.arrForNvShenBang;
        [vc.navigationController pushViewController:zhuBoBangDan animated:YES];
    }
    if (indexPath.row == 7) {
        ZhuBoBangDanViewController *zhuBoBangDan = [[ZhuBoBangDanViewController alloc]init];
        zhuBoBangDan.arrForZhuBoBangDan = self.arrForNanShenBang;
        [vc.navigationController pushViewController:zhuBoBangDan animated:YES];
    }
    if (indexPath.row == 8) {
        ZhuBoBangDanViewController *zhuBoBangDan = [[ZhuBoBangDanViewController alloc]init];
        zhuBoBangDan.arrForZhuBoBangDan = self.arrForXinRenBang;
        [vc.navigationController pushViewController:zhuBoBangDan animated:YES];
    }
    if (indexPath.row == 9) {
        ZhuBoBangDanViewController *zhuBoBangDan = [[ZhuBoBangDanViewController alloc]init];
        zhuBoBangDan.arrForZhuBoBangDan = self.arrForXinRenBang;
        [vc.navigationController pushViewController:zhuBoBangDan animated:YES];
    }
}




@end
